import Request from "@/lib/request/index.js";
import {
	refreshTokenFn
} from "@/api/login.js";
import storage from "@/utils/storage.js";
import {
	md5
} from "@/utils/md5.js";
import api from "@/config/api.js";
import jwt from '@/js_sdk/t-jwt/jwt.js'
import store from "../store";
import configData from "@/config/config";
let isNavigateTo = false
function cleanStorage() {
	uni.showToast({
		title: "你还未登录系统，请登录",
		icon: "none",
		duration: 1500,
	});
	if (uni.showLoading()) {
		uni.hideLoading();
	}
	storage.setHasLogin(false);
	//storage.setAccessToken("");
	storage.setUserInfo({});
}
let http = new Request()
http.setConfig((config) => {
	/* 设置全局配置 */
	config.baseURL = api.common;
	config.header = {
		...config.header,
	};
	config.validateStatus = (statusCode) => {
		// 不论什么状态,统一在正确中处理
		return true;
	};
	return config;
});

http.interceptors.request.use(
	(config) => {
		let accessToken = storage.getAccessToken();
		if (accessToken) {
			config.header.Authorization =accessToken
		}
		return config;
	},
	(config) => {
		return Promise.reject(config);
	}
);

// 是否正在刷新的标记
let isRefreshing = false;
//重试队列
let requests = [];
// 必须使用异步函数，注意
http.interceptors.response.use(
	async (response) => {
			isNavigateTo = false
			uni.showLoading() ? uni.hideLoading() : ''
			if(parseInt(response.data.code)!==200) {
				uni.showToast({
					title: response.data.msg,
					icon: "none",
					duration: 1500,
				});
				if(response.data.code == 20110) {
					cleanStorage()
				}else{
					uni.showToast({
						title: response.data.msg,
						icon: "none",
						duration: 1500,
					});0
				}
			}else{
				return response.data.data
			}
		},
		(error) => {
			uni.showToast({
				title: response.msg,
				icon: "none",
				duration: 1500,
			});
		}
);

export {
	http
};

export const Method = {
	GET: "GET",
	POST: "POST",
	PUT: "PUT",
	DELETE: "DELETE",
};